package com.hongxun.modules.bss.controller;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hongxun.modules.api.entity.BMaterialQuantitiesEntity;
import com.hongxun.modules.api.service.BMaterialQuantitiesService;
import com.hongxun.modules.bss.entity.*;
import com.hongxun.modules.bss.service.APBaseService;
import com.hongxun.modules.bss.service.DMaterialBuyService;
import com.hongxun.modules.bss.service.DMaterialCostService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hongxun.modules.bss.service.DMaterialOldService;
import com.hongxun.common.utils.PageUtils;
import com.hongxun.common.utils.Query;
import com.hongxun.common.utils.R;




/**
 * 旧材料信息表（杆塔）
 * 
 * @author zkcn
 * @email youeremail@163.com
 * @date 2018-09-04 10:42:47
 */
@RestController
@RequestMapping("/bss/dmaterialold")
public class DMaterialOldController {
	@Autowired
	private DMaterialOldService dMaterialOldService;
	@Autowired
	private DMaterialCostService dMaterialCostService;
	@Autowired
	private APBaseService aPBaseService;
	@Autowired
	private BMaterialQuantitiesService bMaterialQuantitiesService;
	@Autowired
	private DMaterialBuyService dMaterialBuyService;
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
        String wheres =" 1=1 ";
		if(query.get("pGuid")!=null&&!query.get("pGuid").toString().equals(""))
		{
			wheres+=" and  p_guid = '" + query.get("pGuid").toString()+"' ";
		}
		if(query.get("dpGuid")!=null&&!query.get("dpGuid").toString().equals(""))
		{
			wheres+=" and  p_p_guid = '" + query.get("dpGuid").toString()+"' ";
		}
		if(query.get("qName")!=null&&!query.get("qName").toString().equals(""))
		{
			wheres+=" and  m_b_name like  '%" + query.get("qName").toString()+"%' ";
		}
		if(query.get("qLabel")!=null&&!query.get("qLabel").toString().equals(""))
		{
			wheres+=" and  d_label like '%" + query.get("qLabel").toString()+"%' ";
		}
		wheres+=" and  m_b_datastate != '删除' ";

        query.put("filterSql",wheres+" ");
		List<DMaterialOldEntity> dMaterialOldList = dMaterialOldService.queryList(query);
		int total = dMaterialOldService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(dMaterialOldList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{guid}")
	public R info(@PathVariable("guid") String guid){
		DMaterialOldEntity dMaterialOld = dMaterialOldService.queryObject(guid);
		
		return R.ok().put("dMaterialOld", dMaterialOld);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public R save(@RequestBody DMaterialOldEntity dMaterialOld){
		dMaterialOldService.save(dMaterialOld);
		APBaseEntity aPBaseEntity = aPBaseService.queryObject(dMaterialOld.getPGuid());
		Map<String, Object> mapaPOther = new HashMap<>();
		String wheres = " 1=1 ";
		wheres += " and  m_guid  = '" + dMaterialOld.getMMGuid() + "' ";
		wheres += " and  m_q_type =  " + aPBaseEntity.getPBType()  ;
		wheres += " and  m_q_work = 2 ";
		mapaPOther.put("filterSql", wheres + " ");
		List<BMaterialQuantitiesEntity> listBMaterialQuantities=	bMaterialQuantitiesService.queryList(mapaPOther);
		for(BMaterialQuantitiesEntity BMaterialQuantitiesEntity :listBMaterialQuantities)
		{
			DMaterialCostEntity dMaterialCostEntity = new DMaterialCostEntity();
			dMaterialCostEntity.setPPGuid(dMaterialOld.getPPGuid());
			dMaterialCostEntity.setPGuid(dMaterialOld.getPGuid());
			dMaterialCostEntity.setPMGuid(dMaterialOld.getGuid());
			dMaterialCostEntity.setMMGuid(dMaterialOld.getMMGuid());
			dMaterialCostEntity.setMCWork("2");
			dMaterialCostEntity.setMCQuantities(BMaterialQuantitiesEntity.getMQQuantities());
			dMaterialCostEntity.setMCExplain(BMaterialQuantitiesEntity.getMQExplain());
			dMaterialCostEntity.setMCUnit(BMaterialQuantitiesEntity.getMQUnit());
			dMaterialCostEntity.setMCMeterUnit(BMaterialQuantitiesEntity.getMQMeterUnit());
			dMaterialCostEntity.setMCCount(dMaterialOld.getMBCount());
			dMaterialCostEntity.setMCPrice(BMaterialQuantitiesEntity.getMQPrice());
			dMaterialCostEntity.setMCPriceall(Priceall(dMaterialCostEntity.getMCCount(), dMaterialCostEntity.getMCPrice()));
			dMaterialCostEntity.setMCRatio(BMaterialQuantitiesEntity.getMQRatio());
			dMaterialCostEntity.setMCDatastate("新增");
			dMaterialCostEntity.setMMName(dMaterialOld.getMBName());
			dMaterialCostEntity.setMMType(1);
			dMaterialCostEntity.setPMSource(3);
			dMaterialCostService.save(dMaterialCostEntity);
		}
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	public R update(@RequestBody DMaterialOldEntity dMaterialOld){
		dMaterialOldService.update(dMaterialOld);
		Map<String, Object> mapaPOther = new HashMap<>();
		String wheres = " 1=1 ";
		wheres += " and  p_m_guid = '" + dMaterialOld.getGuid() + "' ";
		mapaPOther.put("filterSql", wheres + " ");
		List<DMaterialCostEntity> dMaterialCostList = dMaterialCostService.queryList(mapaPOther);
		for (DMaterialCostEntity dMaterialCostEntity :dMaterialCostList) {
			dMaterialCostEntity.setMCCount(dMaterialOld.getMBCount() );
			dMaterialCostEntity.setMCPriceall(Priceall(dMaterialCostEntity.getMCPrice(),dMaterialCostEntity.getMCCount()));
			dMaterialCostService.update(dMaterialCostEntity);
		}
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	public R delete(@RequestBody String[] guids){
		dMaterialOldService.deleteBatch(guids);
		dMaterialCostService.deleteBatchByMBguid(guids);
		List<DMaterialBuyEntity> dMaterialBuyEntityList =	dMaterialBuyService.queryListByDDguid(guids);
		if(dMaterialBuyEntityList.size()>0)
		{
			String [] d  = new String [dMaterialBuyEntityList.size()];
			for(int i=0;i<dMaterialBuyEntityList.size();i++)
			{
				d[i]=dMaterialBuyEntityList.get(i).getGuid();
			}
			dMaterialBuyService.deleteBatch(d);
			dMaterialCostService.deleteBatchByMBguid(d);
		}


		return R.ok();
	}
	private static BigDecimal Priceall(BigDecimal Price , BigDecimal Count){

		BigDecimal Priceall = null;
		Priceall = Price.multiply(Count);
		return Priceall;
	}
}
